// CSE346P2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <deque>
#include <list>
#include <queue>
#include <time.h>
#include <fstream>
#define BUFFER_SIZE 5;
//Set the buffer size as 5
using namespace std;
bool getRand(double);
//random tlue false generator by percentage given
int _tmain(int argc, _TCHAR* argv[])
{
	/* initialize random seed: */
	srand ( time(NULL) );
	int maxtime = 10000;
	//time slot as 10000
	int bufferSize = BUFFER_SIZE;
	//set buffer size
	double packetServed=0;
	//couter for served packets
	double servingRate = .9;
	//Packet serving rate
	double packetRate = .9;
	//Packet input rate(temporary use only here
	double packetDropped=0;
	//counter for dropped packets
	long packetStayed = 0;
	//counter for packet stayed
	ofstream datafile;
    datafile.open ("data.dat");
	//output file control
	queue<int> buffer;
	datafile << "index," <<"Packet Serving Rate" << ","
		<< "Packet Input Rate" << "," << "Packet Served" ;
	datafile << "," << "Packet Dropped" << "," 
		<< "Average Delay of Queue" << "," << "throughput"<< endl;
	int k= 0;
	for(double j = 0.01; j <=1.0 ; j=j+.01){
		k++;
		packetServed = 0;
		packetDropped = 0;
		packetStayed = 0;
		//init counters

		

		for(int i = 0; i < maxtime ; i++){
	
			if(!buffer.empty() && getRand(servingRate)){
				buffer.pop();
				packetServed++;
			} //departure of the packets in the buffer


			if(getRand(j)){
				if(buffer.size() < 5){
					buffer.push(i);
				}
				else
				{
					packetDropped++;
				}
			} //Arrival of the packets with buffersize 5

			packetStayed = packetStayed + buffer.size();
			//packet buffer size counter
			
		}
		datafile << k <<","<<servingRate << "," << j 
			<< "," << packetServed << "," << packetDropped;
		datafile << "," << packetStayed/packetServed <<","
			<< packetServed/static_cast<double>(maxtime) << endl;
	}
	datafile.close();

	return 0;
}

bool getRand(double percentage){
	double randomNumber = (rand())%10000/static_cast<double>(10000);
	if(randomNumber < percentage){
		return true;
	}else{
		return false;
	}
}